<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>MailMS</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta
            content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"
            name="viewport">
    <!-- Bootstrap 3.3.6 -->
    <link rel="stylesheet"
          href="${pageContext.request.contextPath }/res/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link href="//cdn.bootcss.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">
    <%--<!-- Ionicons -->--%>
    <%--<link rel="stylesheet"--%>
    <%--href="${pageContext.request.contextPath }/res/css/font-awesome.min.css">--%>
    <link href="//cdn.bootcss.com/ionicons/2.0.1/css/ionicons.min.css" rel="stylesheet">
    <%--<link rel="stylesheet"--%>
    <%--href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css">--%>
    <!-- fullCalendar 2.2.5-->
    <link rel="stylesheet"
          href="${pageContext.request.contextPath }/res/css/alet/fullcalendar.min.css">
    <link rel="stylesheet"
          href="${pageContext.request.contextPath }/res/css/alet/fullcalendar.print.css"
          media="print">
    <!-- Theme style -->
    <link rel="stylesheet"
          href="${pageContext.request.contextPath }/res/css/AdminLTE.min.css">
    <!-- AdminLTE Skins. Choose a skin from the css/skins
           folder instead of downloading all of them to reduce the load. -->
    <link rel="stylesheet"
          href="${pageContext.request.contextPath }/res/css/alet/_all-skins.min.css">
    <!-- iCheck -->
    <link rel="stylesheet"
          href="${pageContext.request.contextPath }/res/css/alet/blue.css">
    <link rel="stylesheet"
          href="${pageContext.request.contextPath }/res/css/alet/bootstrap3-wysihtml5.min.css">
    <link rel="stylesheet"
        href="${pageContext.request.contextPath }/res/css/mailbox.css">
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->


</head>
<body class="hold-transition skin-blue sidebar-mini">
<%--进度条模态框--%>
<div class="modal fade" id="schronModel">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">正在同步邮件...</h4>
            </div>
            <div class="modal-body">
                <div class="progress progress-sm active">
                    <div id="progress" class="progress-bar progress-bar-primary progress-bar-striped" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: 0%">
                    </div>
                </div>
                <div class="pull-right" id="progressText">10/30</div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<!-- 同步提示模态框 -->
<div class="modal fade" id="noNeedSynModel">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">邮件同步提示：</h4>
            </div>
            <div class="modal-body">
                <div>
                    暂无新邮件需要同步!
                </div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<div class="wrapper">

    <header class="main-header">
        <!-- Logo -->
        <a href="${pageContext.request.contextPath}\mail\index.shtml" class="logo">
            <span class="logo-mini"><b>M</b>MS</span>
            <span class="logo-lg"><b>Mail</b>MS</span>
        </a>
        <!-- Header Navbar: style can be found in header.less -->
        <nav class="navbar navbar-static-top">
            <!-- Sidebar toggle button-->
            <a href="#" class="sidebar-toggle" data-toggle="offcanvas"
               role="button"> <span class="sr-only">Toggle navigation</span> <span
                    class="icon-bar"></span> <span class="icon-bar"></span> <span
                    class="icon-bar"></span>
            </a>


            <div class="navbar-custom-menu">
                <ul class="nav navbar-nav">
                    <%--用户名信息--%>
                    <li class="dropdown user user-menu"><a href="#"
                                                           class="dropdown-toggle" data-toggle="dropdown"> <span
                            class="hidden-xs">${user.username}
                                                <c:if test="${user} == null">登录</c:if>
                    </span>
                    </a>
                        <ul class="dropdown-menu">
                            <!-- Menu Footer-->
                            <li class="user-footer">
                                <%--<div class="pull-left">--%>
                                <%--<a href="#" class="btn btn-default btn-flat">Profile</a>--%>
                                <%--</div>--%>
                                <div class="pull-right">
                                    <a href="#" class="btn btn-default btn-flat">Sign out</a>
                                </div>
                            </li>
                        </ul>
                    </li>
                    <!-- Control Sidebar Toggle Button -->
                    <li><a href="#" data-toggle="control-sidebar"><i
                            class="fa fa-gears"></i></a></li>
                </ul>
            </div>
        </nav>
    </header>
    <!-- Left side column. contains the logo and sidebar -->
    <aside class="main-sidebar">
        <!-- search form -->
        <form action="#" method="get" class="sidebar-form">
            <div class="input-group">
                <input type="text" name="q" class="form-control"
                       placeholder="Search..."> <span class="input-group-btn">
							<button type="submit" name="search" id="search-btn"
                                    class="btn btn-flat">
                                <i class="fa fa-search"></i>
                            </button>
						</span>
            </div>
        </form>
        <!-- /.search form -->
        <!-- 菜单 -->
        <ul class="sidebar-menu">
            <li class="header" id="listLabel">邮箱列表</li>


                <%--显示邮箱列表--%>



            <li class="header">LABELS</li>
            <li><a href="javascript:void(0);" id="importMail"><i class="fa fa-circle-o text-red"></i> <span>导入邮箱</span></a>
            </li>
            <li><a href="#"><i class="fa fa-circle-o text-yellow"></i>
                <span>设置</span></a></li>
            <li><a href="${pageContext.request.contextPath}/logout.shtml"><i class="fa fa-circle-o text-aqua"></i> <span>注销</span></a></li>
        </ul>
        </section>
        <!-- /.sidebar -->
    </aside>

    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                邮件箱
                <small>有13条新消息</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
                <li class="active">邮件箱</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-md-3">
                    <a href="javascript:void(0)"
                       class="btn btn-primary btn-block margin-bottom" id="sendButton">发送邮件</a>

                    <a href="javascript:void(0)"
                       class="btn btn-primary btn-block margin-bottom" id="syncronButton">同步邮件</a>

                    <div class="box box-solid">
                        <div class="box-header with-border">
                            <h3 class="box-title">邮箱</h3>

                            <div class="box-tools">
                                <button type="button" class="btn btn-box-tool"
                                        data-widget="collapse">
                                    <i class="fa fa-minus"></i>
                                </button>
                            </div>
                        </div>
                        <div class="box-body no-padding">
                            <ul class="nav nav-pills nav-stacked">
                                <li class="active"><a href="javascript:void(0)" id="inbox"><i class="fa fa-inbox"></i>
                                    收件箱 </a></li>
                                <li><a href="javascript:void(0)" id="sendBox"><i class="fa fa-envelope-o"></i> 发件箱</a></li>
                                <li><a href="javascript:void(0)" id="draftBox"><i class="fa fa-file-text-o"></i>
                                    草稿箱</a></li>
                                <li><a href="javascript:void(0)" id="starBox"><i class="fa fa-filter"></i> 星标邮件 </a></li>
                                <li><a href="javascript:void(0)" id="trashBox"><i class="fa fa-trash-o"></i> 回收箱</a></li>
                            </ul>
                        </div>
                        <!-- /.box-body -->
                    </div>
                    <!-- /. box -->

                </div>
                <!-- /.col -->

                <div class="col-md-9">
                    <div class="box box-primary" id="variableContent">
                        <%--页面动态加载--%>


                    </div>
                    <!-- /. box -->
                </div>
                <!-- /.col -->
                <div class="pull-right block-hint" id="newMessageAlert" style="display: none;">
                    <a href="${pageContext.request.contextPath}/mail/index.shtml">您有新的邮件,点击查看!<br>
                        <div id="new-message-from">from</div><br>
                        <div id="new-message-subject">subject</div><br>
                    </a>
                </div>
            </div>

            <!-- /.row -->
        </section>
        <!-- /.content -->
    </div>

    <div class="control-sidebar-bg"></div>
</div>
<!-- ./wrapper -->

<!-- jQuery 2.2.0 -->
<script
        src="${pageContext.request.contextPath }/res/js/jquery-2.1.4.min.js"></script>
<!-- Bootstrap 3.3.6 -->
<script
        src="${pageContext.request.contextPath }/res/js/bootstrap.min.js"></script>
<!-- Slimscroll -->
<script
        src="${pageContext.request.contextPath }/res/js/alet/jquery.slimscroll.min.js"></script>
<!-- FastClick -->
<script
        src="${pageContext.request.contextPath }/res/js/alet/fastclick.js"></script>
<!-- AdminLTE App -->
<script
        src="${pageContext.request.contextPath }/res/js/alet/app.min.js"></script>
<!-- iCheck -->
<%--<script--%>
<%--src="${pageContext.request.contextPath }/res/js/alet/icheck.min.js"></script>--%>
<!-- Page Script -->
<script
        src="${pageContext.request.contextPath }/res/js/alet/bootstrap3-wysihtml5.all.min.js"></script>
<script src="${pageContext.request.contextPath}/res/js/list.js"></script>
<script>

    $(function () {


        //显示maillist
        appendMailList({"page":1,"controller":"inbox"});

        //显示邮箱列表
        function appendBoxList(){
            $.get('${pageContext.request.contextPath}/mail/boxList.shtml', function (data) {
                $("#listLabel").after(data);
            });
        }
        appendBoxList();

        function showUnreadNumber(emailAccount){
            $.post('${pageContext.request.contextPath}/manager/showUnreadNumber.shtml',{"emailAccount":emailAccount}, function (data) {
//                alert(emailAccount);
                if(data > 0){
                    $("#inbox").append("<span class='label label-primary pull-right'>"+data+"</span>");
                }
            });
        }

        showUnreadNumber('${sessionScope.currentEmail.account}');


        var saveDraftInterval ;
        //发送邮件按钮点击事件
        $("#sendButton").click(function () {
            if ($(this).html() == "发送邮件") {
                //显示发送邮件界面
                $(this).html("返回邮件列表");
                $("#variableContent").html("");
                $.get('${pageContext.request.contextPath}/mail/compose.shtml', function (data) {
                    $("#variableContent").append(data);
                });
                //启动定时保存
                saveDraftInterval = setInterval(saveDraft,1000 * 10 );
            } else if ($(this).html() == "返回邮件列表") {
                appendMailList({page: 1,controller:"inbox"});
                if(saveDraftInterval){

                    clearInterval(saveDraftInterval);
                }
            }
        });


        //导入新邮箱
        $("#importMail").click(function () {
            $("#sendButton").html("返回邮件列表");
            $("#variableContent").html("");
            $.get('${pageContext.request.contextPath}/mail/bindMail.shtml', function (data) {
                $("#variableContent").append(data);
            });
        });

        var  totalnumber;
        $("#syncronButton").click(function () {

            <%--$.get('${pageContext.request.contextPath}/mail/synchron.shtml', function (data) {--%>
                <%--getAjaxChangeMailList('${pageContext.request.contextPath}/mail/synchron.shtml',"发送邮件");--%>
            <%--});--%>

            //获取需要同步的数量
            $.get('${pageContext.request.contextPath}/mail/getSynchronTotal.shtml',function (data){
                if(data.totalNumber){
                    $('#schronModel').modal({
                        backdrop:'static',
                        show:true
                    });
                    $('#progressText').html('0/'+data.totalNumber);
                    //alert(JSON.stringify(data));
                    totalnumber = data.totalNumber;
                    batchSynchron(data);
                }else{
                    $('#noNeedSynModel').modal('show');
                }

            })

        });

        //30秒同步一次邮件
        setInterval(function(){
            $.post('${pageContext.request.contextPath}/mail/intervalSynchron.shtml',function (data){
                if(data.newNumber){
                    $('#newMessageAlert').css("display","block");
                    $('#new-message-from').html(data.from);
                    $('#new-message-subject').html(data.subject);

                    setTimeout(function(){
                        $('#newMessageAlert').css("display","none")
                    },10*1000);
                }
            })
        },1000*30);

        function batchSynchron(data){
            if(data.totalNumber > 0){
                $.post('${pageContext.request.contextPath}/mail/batchSynchron.shtml',data,function (data){
                    batchSynchron(data);
                    var percent = (1-data.totalNumber / totalnumber) * 100;
                    $('#progress').css("width",percent +"%");
                    $('#progressText').html((totalnumber-data.totalNumber)+'/'+totalnumber);
                })
            }
            if(data.totalNumber == 0){
                window.location.href="${pageContext.request.contextPath}/mail/index.shtml";
            }
        }

        $("#trash").click(function(){
            getAjaxChangeMailList('${pageContext.request.contextPath}/manager/trash.shtml?page=1','发送邮件');
        });

        function getAjaxChangeMailList(url,sendBtnStr){
            $.get(url, function (data) {
                $("sendButton").html(sendBtnStr);
                $("#variableContent").html("");
                $("#variableContent").append(data);
            });
        }

        //收件箱
        $('#inbox').click(function (){
            appendMailList({"page":1,"controller":"inbox"});
        });


        //发件箱
        $('#sendBox').click(function (){

            appendMailList({"page":1,"controller":"sendBox"});
        });

        //草稿箱

        $('#draftBox').click(function (){
            appendMailList({"page":1,"controller":"draftBox"});
        });

        //星标

        $('#starBox').click(function (){
            appendMailList({"page":1,"controller":"starBox"});
        });

        //回收箱

        $('#trashBox').click(function (){
            appendMailList({"page":1,"controller":"trashBox"});
        });


        //显示星标邮件个数
        $.get('${pageContext.request.contextPath}/manager/getStarNumber.shtml', function (data) {
            if(data){
                $('#starBox').append("<span class='label label-warning pull-right'>"+data+"</span>");
            }
        })

    });


</script>
<!-- AdminLTE for demo purposes -->
<script src="${pageContext.request.contextPath }/res/js/alet/demo.js"></script>
<%-- <script
    src="${pageContext.request.contextPath }/res/js/alet/mail/mailajax.js"></script> --%>
</body>
</html>
